local skel = fk.CreateSkill{
  name = "kt_st__jiguo",
}

Fk:loadTranslationTable{
  ["kt_st__jiguo"] = "机国",
  [":kt_st__jiguo"] = "每轮限一次，当你的体力值变化后，若为全场第二大，你可以获得最大者的两张牌。在你失去这两张牌前，"..
  "其拥有技能“陷嗣”；且在其仍为体力值最大时，你使用其发动“陷嗣”置为的装备牌。",
  ["#kt_st__jiguo"] = "机国：你可以获得体力值最大者的两张牌",
  ["@@kt_st__jiguo-inhand"] = "机国",
  ["@@kt_st__jiguo"] = "机国",

  ["$kt_st__jiguo1"] = "如今之事不在太子，在殿陛相争。",
  ["$kt_st__jiguo2"] = "皇后宠爱杨广，事则有变矣。",
  ["$kt_st__jiguo3"] = "臣当效死，以追伊周之事。",
  ["$kt_st__jiguo4"] = "作壁上观，如何得隆极之位。",
}

skel:addEffect(fk.HpChanged, {
  mute = true,
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(skel.name) and target == player and player:usedSkillTimes(skel.name, Player.HistoryRound) == 0 then
      local room = player.room
      local hplist = {}
      for _,p in ipairs(room:getAlivePlayers()) do
        table.insertIfNeed(hplist,p.hp)
      end
      table.sort(hplist)
      if #hplist == 1 then return end
      return player.hp == hplist[#hplist - 1]
    end
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    local hplist = {}
    for _,p in ipairs(room:getAlivePlayers()) do
      table.insertIfNeed(hplist,p.hp)
    end
    table.sort(hplist)
    local hpmax = hplist[#hplist]
    local players = {}
    for _,p in ipairs(room:getAlivePlayers()) do
      if p.hp == hpmax and not p:isNude() then
        table.insertIfNeed(players,p)
      end
    end
    if #players == 0 then return false end
    local to = room:askToChoosePlayers(player, {
      min_num = 1,
      max_num = 1,
      targets = players,
      skill_name = skel.name,
      prompt = "#kt_st__daizu",
      cancelable = true,
    })
    if #to > 0 then
      event:setCostData(self, {tos = to})
      return true
    end
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    room:notifySkillInvoked(player, skel.name)
    player:broadcastSkillInvoke(skel.name,math.random(1,2))
    local to = event:getCostData(self).tos[1]
    local cid = {}
    if #to:getCardIds("he") <= 2 then
      cid = to:getCardIds("he")
    else
      cid = room:askToChooseCards(player, {
        min = 2,
        max = 2,
        target = to,
        flag = "he",
        skill_name = skel.name,
        cancelable = false,
      })
    end
    room:obtainCard(player, cid, false, fk.ReasonPrey, player, skel.name,"@@kt_st__jiguo-inhand")
    room:setPlayerMark(to,"@@kt_st__jiguo",1)
    room:setPlayerMark(player,"kt_st__jiguo",1)
    room:handleAddLoseSkills(to,"xiansi",skel.name)
  end,
})

skel:addEffect(fk.AfterCardsMove,{
  mute = true,
  can_refresh = function(self, event, target, player, data)
    if player:hasSkill(skel.name,true) then
      for _, move in ipairs(data) do
        if move.from == player then
          for _, info in ipairs(move.moveInfo) do
            if info.fromArea == Card.PlayerHand then
              return true
            end
          end
        end
      end
    end
  end,
  on_refresh = function (self, event, target, player, data)
    local room = player.room
    local val = false
    for _,c in ipairs(player:getCardIds("h")) do
      if Fk:getCardById(c):getMark("@@kt_st__jiguo-inhand") > 0 then
        val = true
        break
      end
    end
    if val then return end
    room:setPlayerMark(player,"kt_st__jiguo",0)
    for _,p in ipairs(room:getAllPlayers()) do
    if p:getMark("@@kt_st__jiguo") > 0 then
      room:setPlayerMark(p,"@@kt_st__jiguo",0)
      room:handleAddLoseSkills(p,"-xiansi",skel.name)
    end
    end
  end,
})

skel:addEffect(fk.AfterCardsMove,{
  mute = true,
  can_trigger = function(self, event, target, player, data)
    local room = player.room
    if player:hasSkill(skel.name,true) and player:getMark("kt_st__jiguo") > 0 then
      for _, move in ipairs(data) do
        if move.skillName == "xiansi" and move.to and move.to:getMark("@@kt_st__jiguo") > 0 and move.toArea == Card.PlayerSpecial then
          for _, info in ipairs(move.moveInfo) do
            local card = Fk:getCardById(info.cardId)
            if card.type == Card.TypeEquip and player:canMoveCardIntoEquip(info.cardId, true) then
              local hplist = {}
              for _,p in ipairs(room:getAlivePlayers()) do
                table.insertIfNeed(hplist,p.hp)
              end
              table.sort(hplist)
              return move.to.hp == hplist[#hplist]
            end
          end
        end
      end
    end
  end,
  on_cost = Util.TrueFunc,
  on_use = function (self, event, target, player, data)
    local room = player.room
    room:notifySkillInvoked(player, skel.name)
    player:broadcastSkillInvoke(skel.name,math.random(3,4))
    for _, move in ipairs(data) do
      if move.skillName == "xiansi" and move.to and move.to:getMark("@@kt_st__jiguo") > 0 and move.toArea == Card.PlayerSpecial then
        for _, info in ipairs(move.moveInfo) do
          local card = Fk:getCardById(info.cardId)
          if card.type == Card.TypeEquip and player:canMoveCardIntoEquip(info.cardId, true) then
            room:useCard({
              card = card,
              from = player,
              tos = {player},
            })
          end
        end
      end
    end
  end,
})


return skel